package de.hauck.rtmidlet.views;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import org.microemu.device.ui.ImageStringItemUI;

import de.hauck.rtmidlet.DownloadImageThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.utils.Utils;

public class CanvasImage extends CanvasSuper implements CommandListener,
		iCanvas {
	private Font f_small, f_med;
	private Image _image;
	private Displayable _dble;
	private Image conn_red, conn_yellow, conn_green, conn_status;
	private String _name = "";
	private String[] _urls;
	private Image[] _images;
	private int _index=0;

	public CanvasImage(Displayable dble, String[] urls) {
		super();
		_dble = dble;
		_urls = urls;
		_images = new Image[urls.length];
		// setFullScreenMode(true);
		// setCommandListener(this);
		// try {
		// conn_red = Image.createImage("/conn_red.png");
		// conn_yellow = Image.createImage("/conn_yellow.png");
		// conn_green = Image.createImage("/conn_green.png");
		// conn_status = conn_red;
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		f_small = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		f_med = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM);

	}

	protected void paint(Graphics g) {
		super.paint(g);
		// g.setColor(0);
		// g.fillRect(0, 0, getWidth(), getHeight());
		// g.setColor(255, 255, 255);
		// g.drawImage(conn_status, getWidth(), 1, Graphics.TOP |
		// Graphics.RIGHT);
		_image=_images[_index];
		if (_image != null) {
			
			g.drawImage(_image, getWidth() / 2, (getHeight() / 2)
					- _image.getHeight() / 2, Graphics.TOP | Graphics.HCENTER);
			g.setFont(f_med);
			g.drawString(_name, getWidth() / 2, getHeight() / 2
					+ _image.getHeight() / 2 + f_med.getHeight(), Graphics.TOP
					| Graphics.HCENTER);
		} else {
			g.setFont(f_small);
			g.drawString("No data available yet", getWidth() / 2,
					(getHeight() / 2), Graphics.TOP | Graphics.HCENTER);
		}
	}

	public void commandAction(Command arg0, Displayable arg1) {
		// TODO Auto-generated method stub

	}

	protected void keyPressed(int keyCode) {
		int action = getGameAction(keyCode);
		if (keyCode == KEY_NUM1) {
			downloadPic(_name, _index);
			this.repaint();
			System.out.println("reload");
		} else if (keyCode == KEY_NUM5 || action == FIRE) {
			System.out.println("change");
			if (_index < _urls.length - 1)
				_index++;
			else
				_index = 0;
			if (null == _images[_index])
				downloadPic(_name, _index);
			else {
				_image = _images[_index];
				this.repaint();
			}
		} else if (keyCode == -6 || keyCode == -7) {
			System.out.println("back");
			RTMidlet.getInstance().show(_dble);
		} else {
			System.out.println("donnu what to do");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.hauck.rtmidlet.views.iCanvas#showPic(javax.microedition.lcdui.Image)
	 */
	public void showPic(Image image) {
		//_image = image;
		if (image.getWidth()>image.getHeight()) {
			try {
				image=Utils.rotateImage(image, 90);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		_images[_index]=image;
		repaint();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.hauck.rtmidlet.views.iCanvas#downloadPic(java.lang.String,
	 * java.lang.String)
	 */
	public void downloadPic(String name, int index) {
		_name = name;
		_index = index;
		_image = null;
		repaint();
		System.out.println("download " + _urls[index]);
		new DownloadImageThread(_urls[index], 0, this);
	}

	public void setUrls(String[] urls) {
		_urls = urls;
		_images = new Image[urls.length];
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.hauck.rtmidlet.views.iCanvas#updateStatus(int)
	 */
	// public void updateStatus(int i) {
	// switch (i) {
	// case 0: {
	// conn_status = conn_red;
	// repaint();
	// break;
	// }
	// case 1: {
	// conn_status = conn_yellow;
	// repaint();
	// break;
	// }
	// case 2: {
	// conn_status = conn_green;
	// repaint();
	// break;
	// }
	// }
	// }

}
